﻿ using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using BADDGUI.Utils.SQLHelper;
using BADDGUI.Controls;

namespace BADDGUI
{
    public partial class frmFmam : BLCForm
    {
        DataRow drSelectedMenu;
        DataView dvNodes;

        public frmFmam()
        {
            InitializeComponent();
            InitializeUI();
            InitializeData();
        }

        private void frmFmnd_Load(object sender, EventArgs e)
        {
            tabMenu.SelectedIndex = 0;
            tabMenu_SelectedIndexChanged(null, null);
        }

        public void InitializeUI()
        {
            InitTab();
            InitDataGrid();
        }

        public Object DataSource
        {
            get { return this.DataGrid.DataSource; }
            set
            {
                this.DataGrid.DataSource = value;    
            }
        }

        public void InitDataGrid()
        {
            this.DataGrid = new BLCDataGridView(this);

            ((System.ComponentModel.ISupportInitialize)(this.DataGrid)).BeginInit();
            this.SuspendLayout();

            DataTable dtSchema = BLCGlobalData.BASLOG.GetAViewSchema(this.FormView);

            int i = 0;
            foreach (DataRow row in dtSchema.Rows)
            {
                i++;
                DataGridViewColumn mydgvCol = new DataGridViewTextBoxColumn();
                mydgvCol.Name = "col" + row["ColumnName"].ToString().Replace(" ", string.Empty);
                mydgvCol.HeaderText = row["ColumnName"].ToString();
                mydgvCol.DataPropertyName = row["ColumnName"].ToString();
                mydgvCol.ValueType = Type.GetType(row["DataType"].ToString());
                mydgvCol.SortMode = DataGridViewColumnSortMode.Programmatic;
                this.DataGrid.Columns.Add(mydgvCol);
                if (i > 30) break;
            }

            blcPnlGrid.Controls.Add(this.DataGrid);
            
            ((System.ComponentModel.ISupportInitialize)(this.DataGrid)).EndInit();
            this.ResumeLayout(false);
        }
        
        public void InitTab()
        {
            foreach (DataRow drMenu in BLCGlobalData.DtMenu.Rows)
            {
                TabPage mytab = new TabPage((string)drMenu["Module Name"]);
                mytab.Name = "tab" + (string)drMenu["Module Code"];
                mytab.Tag = drMenu["Trx No"];
                tabMenu.TabPages.Add(mytab);
            }
        }

        public void InitializeData()
        {
            dvNodes = BLCGlobalData.DtNodes.DefaultView;
            dvNodes.Sort = "Sort ASC";
        }

        private void tabMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            drSelectedMenu = BLCGlobalData.DtMenu.Rows
                                .Cast<DataRow>()
                                .Where(x =>
                                    (int)x["Trx No"] == (int)tabMenu.TabPages[tabMenu.SelectedIndex].Tag).Single();

            dvNodes.RowFilter = string.Format("[Module Trx No] = {0}", (int)tabMenu.TabPages[tabMenu.SelectedIndex].Tag);
            this.DataGrid.DataSource = dvNodes;
        }

        private void btnColumnSetting_Click(object sender, EventArgs e)
        {

        }
    }
}